package com.mission.schedule.appwidget;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.entity.CLRepeatTable;
import com.mission.schedule.entity.ScheduleTable;
import com.mission.schedule.utils.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by liwei on 2016/10/24.
 */

public class WidgetSetService extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new widgetFactory(this.getApplicationContext(), intent);
    }

    private class widgetFactory implements RemoteViewsFactory {

        private List<Map<String, String>> mList = new ArrayList<Map<String, String>>();
        List<Map<String, String>> unlist = new ArrayList<Map<String, String>>();
        List<Map<String, String>> todaylist = new ArrayList<Map<String, String>>();
        List<Map<String, String>> todaylist2 = new ArrayList<Map<String, String>>();
        List<Map<String, String>> endlist = new ArrayList<Map<String, String>>();
        private Context context;

        private int mAppWidgetId;

        //这个方法用以初始化数据，它在桌面插件被创建的时候会调用
        public widgetFactory(Context context, Intent intent) {
            this.context = context;
            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
            loaddata();
        }

        @Override
        public void onCreate() {
        }

        @Override
        public void onDataSetChanged() {
            loaddata();
        }

        @Override
        public void onDestroy() {

        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public RemoteViews getViewAt(int position) {


            RemoteViews rviews = new RemoteViews(context.getPackageName(), R.layout.widgeta_item);

            if (mList.size() > 0) {
//                Log.e("TAG", "WidgetSetService--getViewAt--" + position);
                if ("0".equals(mList.get(position).get(ScheduleTable.schDisplayTime))
                        && "1".equals(mList.get(position).get(ScheduleTable.schIsPostpone))) {
                    rviews.setTextViewText(R.id.time_item, "待办");
                } else if ("0".equals(mList.get(position).get(ScheduleTable.schDisplayTime))
                        && "0".equals(mList.get(position).get(ScheduleTable.schIsPostpone))) {
                    rviews.setTextViewText(R.id.time_item, "全天");
                } else {
                    rviews.setTextViewText(R.id.time_item, mList.get(position).get(ScheduleTable.schTime));
                }


                rviews.setViewVisibility(R.id.xcx_tv, View.GONE);
                if (!"0".equals(mList.get(position).get(ScheduleTable.schRepeatID))) {
                    Map<String, String> map = App.getDBcApplication().QueryhongFuData(mList.get(position).get(ScheduleTable.schRepeatID));
                    if (map.get(CLRepeatTable.remark2).equals("100")
                            || map.get(CLRepeatTable.remark2).equals("101")
                            || map.get(CLRepeatTable.remark2).equals("102")
                            || map.get(CLRepeatTable.remark2).equals("103")
                            || map.get(CLRepeatTable.remark2).equals("104")
                            || map.get(CLRepeatTable.remark2).equals("118")
                            || map.get(CLRepeatTable.remark2).equals("121")
                            || map.get(CLRepeatTable.remark2).equals("122")) {
                        rviews.setViewVisibility(R.id.xcx_tv, View.VISIBLE);
                    } else {
                        rviews.setViewVisibility(R.id.xcx_tv, View.GONE);
                    }
                }

                rviews.setTextViewText(R.id.content_item, mList.get(position).get(ScheduleTable.schContent));

                rviews.setTextColor(R.id.time_item, Integer.valueOf(mList.get(position).get(ScheduleTable.schIsEnd)) == 1 ?
                        context.getResources().getColor(R.color.gongkai_txt4) : context.getResources().getColor(R.color.white));
                rviews.setTextColor(R.id.content_item, Integer.valueOf(mList.get(position).get(ScheduleTable.schIsEnd)) == 1 ?
                        context.getResources().getColor(R.color.gongkai_txt4) : context.getResources().getColor(R.color.white));
                if (Integer.valueOf(mList.get(position).get(ScheduleTable.schcRecommendId)) > 0
                        && Integer.valueOf(mList.get(position).get(ScheduleTable.schAID)) == 0) {
                    if (!mList.get(position).get(ScheduleTable.schRepeatID).equals("0")){
//                        rviews.setViewVisibility(R.id.name_item, View.GONE);
                    }else {
//                        rviews.setViewVisibility(R.id.name_item, View.VISIBLE);
//                        rviews.setTextViewText(R.id.name_item, "@" + mList.get(position).get(ScheduleTable.schcRecommendName) + ": ");
                        rviews.setTextColor(R.id.content_item, Integer.valueOf(mList.get(position).get(ScheduleTable.schIsEnd)) == 1 ?
                        context.getResources().getColor(R.color.gongkai_txt4) : context.getResources().getColor(R.color.endbackground));
                    }
                } else {
//                    rviews.setViewVisibility(R.id.name_item, View.GONE);
                }
//                rviews.setTextColor(R.id.name_item, Integer.valueOf(mList.get(position).get(ScheduleTable.schIsEnd)) == 1 ?
//                        context.getResources().getColor(R.color.gongkai_txt) : context.getResources().getColor(R.color.endbackground));

                rviews.setImageViewResource(R.id.check_item, mList.get(position).get(ScheduleTable.schIsEnd).equals("1")
                        ? R.mipmap.btn_gou : R.mipmap.icon_santai_w);
//                if (mList.get(position).get(ScheduleTable.schpisEnd).equals("1")) {
//                    rviews.setImageViewResource(R.id.check_item, R.mipmap.btn_gou);
//                }

                String key = mList.get(position).get(ScheduleTable.schDate);
                String timeKey = mList.get(position).get(ScheduleTable.schTime);

                Date dateStr = DateUtil.parseDateTime(key + " " + timeKey);

                Date date1 = DateUtil.parseDateTime((DateUtil
                        .formatDateTime(new Date())));
                /**
                 * 待办隐藏红点,非待办判断时间是否大于当前来隐藏红点
                 */
                if ("0".equals(mList.get(position).get(
                        ScheduleTable.schDisplayTime))
                        && "1".equals(mList.get(position).get(
                        ScheduleTable.schIsPostpone))) {
                    rviews.setViewVisibility(R.id.guoqi_img, View.GONE);
                }else {
                    if (dateStr.getTime() < date1.getTime()) {
                        if ("1".equals(mList.get(position).get(ScheduleTable.schIsEnd))) {
                            rviews.setViewVisibility(R.id.guoqi_img, View.GONE);
                        } else {
                            rviews.setViewVisibility(R.id.guoqi_img, View.VISIBLE);
                        }
                    } else {
                        rviews.setViewVisibility(R.id.guoqi_img, View.GONE);
                    }
                }

            }

            Bundle extras = new Bundle();
            extras.putInt(WidgetA.EXTRA_BUTTON_ITEM, position);
            Intent intent = new Intent();
            intent.putExtras(extras);
            if (mList.get(position).get(ScheduleTable.schpisEnd).equals("0")) {
                rviews.setOnClickFillInIntent(R.id.check_ll, intent);
            }


            return rviews;
        }

        @Override
        public RemoteViews getLoadingView() {
            return null;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }


        private void loaddata() {
            try {
                unlist.clear();
                todaylist.clear();
                mList.clear();
                endlist.clear();

                unlist = App.getDBcApplication().queryAllSchData(901, 0, 0);
                todaylist = App.getDBcApplication().queryAllSchData(907, 0, 0);

                mList.addAll(unlist);
                mList.addAll(todaylist);

                ArrayList<Map<String, String>> list = App.getDBcApplication().queryAllSchData(-4, 0, 0);
                if (mList.size() == 0 && list.size() > 0) {
                    for (int i = 0; i< 7; i++) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.add(Calendar.DATE, +i);
                        String date = DateUtil.formatDate(calendar.getTime());
                        mList.addAll(App.getDBcApplication().queryCalenderData(date));

                        if (mList.size() > 0) {
                            break;
                        }
                    }
                }

                for (int i = 0; i < mList.size(); i++) {
                    if (Integer.valueOf(mList.get(i).get(ScheduleTable.schIsEnd)) == 1) {
                        endlist.add(mList.get(i));
                    }
                }
                for (int i = 0; i < endlist.size(); i++) {
                    for (int j = 0; j < mList.size(); j++) {
                        if (endlist.get(i).get(ScheduleTable.schID).equals(mList.get(j).get(ScheduleTable.schID))) {
                            mList.remove(j);
                        }
                    }
                }
                if (endlist.size() > 0) {
                    mList.addAll(endlist);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
